import Taro from "@tarojs/taro"
import { appStore } from "@/stores/index"
import { SystemInfoTypes } from "@/types"
/**
 * 预览图片
 * @param imgUrls Array 图片数组
 * @param index number 图片的索引
 * @param show boolean 是否显示菜单
 */
export const previewImage = async (imgUrls:Array<string> = [],index:number = 0,show:boolean = true)=>{
  const imgUrl = imgUrls[index]
  await Taro.previewImage({
    current: imgUrl,
    urls: imgUrls,
    showmenu:show
  })
}

/**
 * 设置setGlobalData信息
 * @returns
 */
export const getSystem = async ()=>{
  const store = appStore()
  const system = await Taro.getSystemInfoSync();
  const { statusBarHeight, screenWidth, screenHeight } = system
  // 获取胶囊信息
  const { width, height, left, top, right } = await Taro.getMenuButtonBoundingClientRect()
  //边距，两边的
  const marginSides = screenWidth - right
  //标题宽度
  const titelBarWidth = screenWidth - width - marginSides * 3
  // 计算标题栏高度
  const titleBarHeight = height + (top - (statusBarHeight??0))*2
  // 计算导航栏高度
  const appHeaderHeight = (statusBarHeight??0) + titleBarHeight

  //去掉导航栏，屏幕剩余的高度
  const contentHeight = screenHeight - appHeaderHeight

  const options: SystemInfoTypes =  {
    system,//系统信息
    statusBarHeight: statusBarHeight,
    titleBarHeight: titleBarHeight,  //标题栏高度
    titleBarWidth: titelBarWidth,  //标题栏宽度
    appHeaderHeight: appHeaderHeight, //整个导航栏高度
    marginSides: marginSides, //侧边距
    capsuleWidth: width, //胶囊宽度
    capsuleHeight: height, //胶囊高度
    capsuleLeft: left,
    contentHeight: contentHeight
  }
  await store.setSystemInfo(options)
}


